package is.client.controller.command;

import is.client.Main;
import is.common.interfaces.Command;

import java.io.IOException;

/**
 * @author aspr0
 *         Date: 12.05.12
 *         Time: 16:26
 */
public class FindCommand implements Command
{
    private String[] masks;
    private int sectionNum;

    public FindCommand(int sectionNum, String[] masks)
    {
        this.masks = masks;
        this.sectionNum = sectionNum;
    }

    @Override
    public void execute()
    {
        int emptyMasksCount = 0;
        for (String mask : masks)
        {
            if (mask.isEmpty())
            {
                emptyMasksCount++;
            }
        }

        if (masks.length - emptyMasksCount > 0)
        {
            try
            {
                String[] header = Main.getController().getModel().getHeader(sectionNum);
                String[][] foundRows = Main.getController().getModel().findRows(sectionNum, masks);

                Main.getController().getUserInterface().exposeRows(sectionNum, header, foundRows);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
    }
}
